﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;
using System.Web;
using BLL;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using Model;

public partial class ViewbyProduct_ZTEElectronicsList_PackageProductSNExportSingle : System.Web.UI.Page
{
    private string packageProductSN { get { return Session["packageProductSN"].ToString(); } set { Session["packageProductSN"] = value; } }
    private PackageProduct pp;
    private Test test;
    private TestItem ti;
    private TestInfo tinfo;

    public ViewbyProduct_ZTEElectronicsList_PackageProductSNExportSingle()
    {
        test = new Test();
        ti = new TestItem();
        pp = new PackageProduct();
    }

    protected void Page_Load(object sender, EventArgs e)
    {
        this.CrystalReportViewer1.Visible = false;
        this.Page.Unload += new EventHandler(Page_Unload);
    }

    void Page_Unload(object sender, EventArgs e)
    {
        this.CrystalReportSource1.Dispose();
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        //此处输入确定为成品序列号
        packageProductSN = this.TextBox1.Text.Trim();
        string ReportFile = Server.MapPath("~/ViewTest/ViewYourSelf/ViewTestItem.rpt");
        string ExcelFileName = Server.MapPath("~/ZedGraphImages/ZTESParamTestData.pdf");
        if (!ExportToExcel(ReportFile, ExcelFileName))
        {
            string str = "没有“" + packageProductSN + "”的测试数据，或无效的成品序列号";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
        }
    }

    public bool ExportToExcel(string ReportFile, string ExcelFileName)
    {
        try
        {
            ReportDocument ReportDOC = new ReportDocument();
            DiskFileDestinationOptions FileOPS = new DiskFileDestinationOptions();
            ExportOptions ExOPS;
            ReportDOC.Load(ReportFile);

            //string productSN = CommHelper.GetProductSN(packageProductSN);
            string productSN = pp.GetPackageProductbyPackageProductSN(packageProductSN).ProductSN;
            if (string.IsNullOrEmpty(productSN))
            {
                throw new Exception("没有相应测试数据!");
            }
            else
            {
                tinfo = test.GetTestbyLastSN(productSN);

                TextObject txtProductSN = (TextObject)ReportDOC.ReportDefinition.ReportObjects["Text4"];
                txtProductSN.Text = "成品序列号：" + tinfo.ProductSN;
                TextObject txtTestTime = (TextObject)ReportDOC.ReportDefinition.ReportObjects["Text13"];
                txtTestTime.Text = "测试时间：" + tinfo.TestTime;
                TextObject txtProductTypeID = (TextObject)ReportDOC.ReportDefinition.ReportObjects["Text14"];
                txtProductTypeID.Text = "产品类型：" + tinfo.ProductTypeID;
                TextObject txtUserID = (TextObject)ReportDOC.ReportDefinition.ReportObjects["Text15"];
                txtUserID.Text = "测试员工号：" + tinfo.UserID;
                TextObject txtENAID = (TextObject)ReportDOC.ReportDefinition.ReportObjects["Text16"];
                txtENAID.Text = "矢网编号：" + tinfo.ENAID;
                TextObject txtSpecVersion = (TextObject)ReportDOC.ReportDefinition.ReportObjects["Text2"];
                txtSpecVersion.Text = "指标单版本：" + tinfo.SpecVersion;
                TextObject txtIsPass = (TextObject)ReportDOC.ReportDefinition.ReportObjects["Text17"];
                txtIsPass.Text = "是否通过：" + tinfo.IsPass;

                IList<TestItemInfo> dti = ti.GetTestItems(productSN, tinfo.TestTime);
                ReportDOC.SetDataSource(dti);

                FileOPS.DiskFileName = ExcelFileName;
                ExOPS = ReportDOC.ExportOptions;
                ExOPS.DestinationOptions = FileOPS;
                ExOPS.ExportDestinationType = CrystalDecisions.Shared.ExportDestinationType.DiskFile;
                ExOPS.ExportFormatType = CrystalDecisions.Shared.ExportFormatType.PortableDocFormat;
                ReportDOC.Export();
                Response.Clear();
                Response.Buffer = true;
                EnableViewState = false;
                string filename = packageProductSN + ".pdf";
                Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(filename, Encoding.UTF8));
                Response.WriteFile(ExcelFileName);
                Response.Flush();
                Response.End();
                Response.Close();
                return true;
            }
        }
        catch
        {
            return false;
        }
    }

    protected void Button1_Click(object sender, EventArgs e)
    {
        //此处输入确定为成品序列号
        packageProductSN = this.TextBox1.Text.Trim();
        ForCrystalReport(packageProductSN);
    }

    protected void ForCrystalReport(string packageProductSN)
    {
        CrystalReportSource1.ReportDocument.Load(Server.MapPath("~/ViewTest/ViewYourSelf/ViewTestItem.rpt"));

        //string productSN = CommHelper.GetProductSN(packageProductSN);
        string productSN = pp.GetPackageProductbyPackageProductSN(packageProductSN).ProductSN;
        if (string.IsNullOrEmpty(productSN))
        {
            string str = "此成品序列号“" + packageProductSN + "”没有关联过腔体序列号";
            ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
            return;
        }
        else
        {
            tinfo = test.GetTestbyLastSN(productSN);
            if (string.IsNullOrEmpty(tinfo.ProductSN))
            {
                string str = "没有" + packageProductSN + "的测试数据";
                ClientScript.RegisterStartupScript(ClientScript.GetType(), "", "<script>alert('" + str + "');</script>");
                return;
            }
            else
            {
                TextObject txtProductSN = (TextObject)this.CrystalReportSource1.ReportDocument.ReportDefinition.ReportObjects["Text4"];
                txtProductSN.Text = "成品序列号：" + tinfo.ProductSN;
                TextObject txtTestTime = (TextObject)this.CrystalReportSource1.ReportDocument.ReportDefinition.ReportObjects["Text13"];
                txtTestTime.Text = "测试时间：" + tinfo.TestTime;
                TextObject txtProductTypeID = (TextObject)this.CrystalReportSource1.ReportDocument.ReportDefinition.ReportObjects["Text14"];
                txtProductTypeID.Text = "产品类型：" + tinfo.ProductTypeID;
                TextObject txtUserID = (TextObject)this.CrystalReportSource1.ReportDocument.ReportDefinition.ReportObjects["Text15"];
                txtUserID.Text = "测试员工号：" + tinfo.UserID;
                TextObject txtENAID = (TextObject)this.CrystalReportSource1.ReportDocument.ReportDefinition.ReportObjects["Text16"];
                txtENAID.Text = "矢网编号：" + tinfo.ENAID;
                TextObject txtSpecVersion = (TextObject)this.CrystalReportSource1.ReportDocument.ReportDefinition.ReportObjects["Text2"];
                txtSpecVersion.Text = "指标单版本：" + tinfo.SpecVersion;
                TextObject txtIsPass = (TextObject)this.CrystalReportSource1.ReportDocument.ReportDefinition.ReportObjects["Text17"];
                txtIsPass.Text = "是否通过：" + tinfo.IsPass;


                IList<TestItemInfo> dti = ti.GetTestItems(productSN, tinfo.TestTime);
                CrystalReportSource1.ReportDocument.SetDataSource(dti);
                CrystalReportSource1.DataBind();
                this.CrystalReportViewer1.ReportSource = CrystalReportSource1;
                CrystalReportViewer1.DataBind();
                this.CrystalReportViewer1.Visible = true;
            }
        }
    }

    protected void CrystalReportViewer1_Navigate(object source, CrystalDecisions.Web.NavigateEventArgs e)
    {
        ForCrystalReport(packageProductSN);
    }
}
